package com.btnc.observer;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.log4j.Logger;

public class TraffyInfoPoller extends TimerTask{

	private static final Logger logger = Logger
			.getLogger(TraffyInfoPoller.class);	

	@Override
	public void run() {
		try {
			getIncidentMessageFlow();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}

	}
	
	public void getIncidentMessageFlow() throws Exception {
		ActiveMQConnection connection = ActiveMQConnection
				.makeConnection("tcp://localhost:61617");
		connection.start();
		Session session = connection.createSession(false,
				Session.AUTO_ACKNOWLEDGE);
		Destination destination = session.createQueue("traffy.input");
		TextMessage traffyMessage = session.createTextMessage();
		traffyMessage
				.setText("<traffy type=\"poller\"><method>getIncident</method><limit>1</limit></traffy>");
		MessageProducer producer = session.createProducer(destination);
		logger.info("Polling for Incident.");
		producer.send(traffyMessage);
		producer.close();
		session.close();
		connection.close();
	}

	public static void main(String[] args){
		TraffyInfoPoller infoPoller = new TraffyInfoPoller();
		
		Timer timer = new Timer();
		timer.scheduleAtFixedRate(infoPoller, new Date(), 60000);
	}

}
